﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Game
{
    public class MySprite
    {
        //private Texture2D[] _texture2d;

        //public Texture2D[] texture2d
        //{
        //    get { return _texture2d; }
        //    set
        //    {
        //        _texture2d = value;
        //        _ntexture2d = _texture2d.Length;
        //    }
        //}
        //private int _ntexture2d;

        //public int ntexture2d
        //{
        //    get { return _ntexture2d; }
        //    set { _ntexture2d = value; }
        //}

        //private float _x;

        //public float x
        //{
        //    get { return _x; }
        //    set { _x = value; }
        //}
        //private float _y;

        //public float y
        //{
        //    get { return _y; }
        //    set { _y = value; }
        //}

        //protected int _itexture2d;

        //protected int itexture2d
        //{
        //    get { return _itexture2d; }
        //    set { _itexture2d = value; }
        //}

        //public MySprite(Texture2D[] _inputtexture2d, float _inputx, float _inputy)
        //{
        //    texture2d = _inputtexture2d;
        //    x = _inputx;
        //    y = _inputy;
        //    itexture2d = 0;
        //}
        //public void Update(GameTime gameTime)
        //{
        //    Random r = new Random();
        //    _x = _x + r.Next(7) - 3;
        //    _y = _y + r.Next(11) - 5;
        //    _itexture2d = (_itexture2d + 1) % _ntexture2d;
        //}

        //public void Draw(GameTime gameTime,  SpriteBatch spriteBatch)
        //{
        //    spriteBatch.Draw(_texture2d[_itexture2d], new Vector2(_x, _y), Color.White);
        //    //g.DrawImage(_texture2d[_itexture2d], _x, _y);

        //}

        public MySprite(Texture2D _inputtexture2d, int _inputnframe, int _inputdframe, float _inputx, float _inputy)
        {
            texture2d = _inputtexture2d;
            x = _inputx;
            y = _inputy;
            iFrame = 0;
            _nFrame = _inputnframe;// so frame hang ngang
            _dFrame = _inputdframe;// so frame hang doc
            _frameWidth = _inputtexture2d.Width / _inputnframe;
            _frameHeight = _inputtexture2d.Height / _inputdframe;
        }



        private Texture2D _texture2d;


        public Texture2D texture2d
        {
            get { return _texture2d; }
            set
            {
                _texture2d = value;
                //_ntexture2d = _texture2d.Length;
            }
        }
        //-----------------------------------
        private int _frameWidth;

        public int FrameWidth
        {
            get { return _frameWidth; }
            set { _frameWidth = value; }
        }

        private int _frameHeight;

        public int FrameHeight
        {
            get { return _frameHeight; }
            set { _frameHeight = value; }
        }
        //-----------------------------------


        private int _nFrame;
        public int nFrame
        {
            get { return _nFrame; }
            set { _nFrame = value; }
        }
        private int _dFrame;

        public int dFrame
        {
            get { return _dFrame; }
            set { _dFrame = value; }
        }



        private float _x;

        public float x
        {
            get { return _x; }
            set { _x = value; }
        }
        private float _y;

        public float y
        {
            get { return _y; }
            set { _y = value; }
        }

        //private int _itexture2d;
        //public int itexture2d
        //{
        //    get { return _itexture2d; }
        //    set { _itexture2d = value; }
        //}
        private int _iFrame;
        public int iFrame
        {
            get { return _iFrame; }
            set { _iFrame = value; }
        }



        public virtual void Update(GameTime gameTime)
        {
            if (Game1.time == 10)
            {
                _iFrame = (_iFrame + 1) % (_nFrame * _dFrame);
                Game1.time = 0;
            }
            else Game1.time++;
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(_texture2d[_itexture2d], new Vector2(_x, _y), 
            //    new Color(255, 255, 255, _transparent));
            
            spriteBatch.Draw(_texture2d, new Vector2(_x, _y), new Rectangle((_iFrame % _nFrame) * _frameWidth, (_iFrame / _nFrame) * _frameHeight, _frameWidth, _frameHeight), Color.White);
            //spriteBatch.Draw(_texture2d, new Vector2(_x, _y), new Rectangle(_iFrame  * _frameWidth, 1, _frameWidth, _frameHeight), Color.White);
            
        }
    }
}
